package com.bst.web.base;

import java.util.*;

import com.bst.base.service.IBaseLabelService;
import com.bst.common.utils.StringUtils;
import com.bst.md.vo.MdIndexParamVO;
import com.bst.system.domain.BaseLabel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.bst.common.annotation.Log;
import com.bst.common.enums.BusinessType;
import com.bst.md.domain.MdIndex;
import com.bst.md.service.IMdIndexService;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.utils.poi.ExcelUtil;
import com.bst.common.core.page.TableDataInfo;

/**
 * 指标信息Controller
 * 
 * @author ruoyi
 * @date 2022-06-18
 */
@Controller
@RequestMapping("/base/md/index")
public class MdIndexController extends BaseController
{
    private String prefix = "base/base/md/index";

    @Autowired
    private IMdIndexService mdIndexService;
    @Autowired
    private IBaseLabelService baseLabelService;

    @GetMapping()
    public String index(ModelMap mmap)
    {
        BaseLabel label = new BaseLabel();
        label.setFgSys(1);
        List<BaseLabel> labels = baseLabelService.selectBaseLabelList(label);
        Map<Long,List<BaseLabel>> dataMap = new HashMap<>();
        Map<Long,String> parentMap = new LinkedHashMap<>();
        for (BaseLabel baseLabel : labels) {
            if(baseLabel.getParentId()==null) {
                dataMap.put(baseLabel.getIdLabel(),new ArrayList<>());
                parentMap.put(baseLabel.getIdLabel(),baseLabel.getNa());
            }
        }
        List<BaseLabel> list;
        for (BaseLabel baseLabel : labels) {
            if(baseLabel.getParentId()!=null) {
                if((list=dataMap.get(baseLabel.getParentId()))!=null) {
                    list.add(baseLabel);
                }
            }
        }
        Map<String,List<BaseLabel>> newDataMap = new HashMap<>();
        for(Map.Entry<Long,List<BaseLabel>> entry:dataMap.entrySet()) {
            newDataMap.put(parentMap.get(entry.getKey()),entry.getValue());
        }
        mmap.put("labels",newDataMap);
        return prefix + "/index";
    }

    /**
     * 查询指标信息列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MdIndex mdIndex)
    {
        startPage();
        List<MdIndex> list = mdIndexService.selectMdIndexList(mdIndex);
        return getDataTable(list);
    }

    /**
     * 查询指标信息列表
     */
    @PostMapping("/listByIdLabels")
    @ResponseBody
    public TableDataInfo listByIdLabels(MdIndexParamVO paramVO)
    {
        startPage();
        List<MdIndex> list = mdIndexService.selectMdIndexListByIdLabels(paramVO);
        return getDataTable(list);
    }

    /**
     * 查询指标信息列表
     */
    @PostMapping("/notInDmList")
    @ResponseBody
    public TableDataInfo notInDmList(MdIndex mdIndex)
    {
        startPage();
        List<MdIndex> list = mdIndexService.selectNotInDm(mdIndex);
        return getDataTable(list);
    }

    /**
     * 导出指标信息列表
     */
    @Log(title = "指标信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MdIndex mdIndex)
    {
        List<MdIndex> list = mdIndexService.selectMdIndexList(mdIndex);
        ExcelUtil<MdIndex> util = new ExcelUtil<MdIndex>(MdIndex.class);
        return util.exportExcel(list, "指标信息数据");
    }

    /**
     * 新增指标信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存指标信息
     */
    @Log(title = "指标信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MdIndex mdIndex)
    {
        return toAjax(mdIndexService.insertMdIndex(mdIndex));
    }

    /**
     * 修改指标信息
     */
    @GetMapping("/edit/{idIndex}")
    public String edit(@PathVariable("idIndex") String idIndex, ModelMap mmap)
    {
        MdIndex mdIndex = mdIndexService.selectMdIndexByIdIndex(idIndex);
        mmap.put("mdIndex", mdIndex);
        return prefix + "/edit";
    }

    /**
     * 修改保存指标信息
     */
    @Log(title = "指标信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MdIndex mdIndex)
    {
        return toAjax(mdIndexService.updateMdIndex(mdIndex));
    }

    /**
     * 删除指标信息
     */
    @Log(title = "指标信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mdIndexService.deleteMdIndexByIdIndexs(ids));
    }

    @GetMapping(value = { "/labelTree"})
    public String selectLabelTree(String idIndex, ModelMap mmap)
    {
        mmap.put("idIndex",idIndex);
        return prefix + "/labelTree";
    }
}
